package interfaces.hud.end;

import interfaces.GUISource;
import interfaces.superWidgets.InterfaceLabel;
import interfaces.superWidgets.InterfaceWindow;
import interfaces.superWidgets.ListenerKeeperElement;
import interfaces.tables.TeamTable;
import logic.common.game.Game;
import logic.common.team.Team;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.decorator.background.PlainBackground;
import org.fenggui.decorator.border.PlainBorder;
import org.fenggui.layout.StaticLayout;
import org.fenggui.util.Color;
import org.fenggui.util.Spacing;

public class ScoreBoardWindow extends InterfaceWindow {
	
	public static final int BORDER_FACTOR = 15;
	
	protected Game game;
	protected TeamTable tableA, tableB;
	protected ListenerKeeperElement element;
	
	protected ImageFont labelFont;
	protected int labelHeight;
	
	public ScoreBoardWindow(Game game, int width, int height, int x, int y, ListenerKeeperElement element) {
		super(false, "");
		this.game = game;
		this.element = element;
		
		setSize(width, height);
		setXY(x, y);
		
		content.setLayoutManager(new StaticLayout());
		content.getAppearance().add(new PlainBackground(Color.BLACK));
		layout();
		
		int spacing = 5;
		getAppearance().setBorder(new Spacing(spacing , spacing));
		getAppearance().add(new PlainBorder(Color.BLUE, spacing));
		
		createTables();
		
		layout();
	}
	
	private void createTables() {
		labelFont = GUISource.bigFont;
		labelHeight = labelFont.getHeight() / 3 * 4;
		
		int spacing = content.getWidth() / 70;
		int tableHeight = content.getHeight() - labelHeight;
		int tableWidth = (content.getWidth() / 2) - spacing;
		
		tableA = new TeamTable(tableWidth, tableHeight, 0, 0, game.getTeamA(), element);
		addWidget(tableA);
		
		tableB = new TeamTable(tableWidth, tableHeight, tableWidth + (spacing * 2), 0, game.getTeamB(), element);
		addWidget(tableB);
		
		addLabel(game.getTeamA(), tableWidth / 2);
		addLabel(game.getTeamB(), tableB.getX() + tableWidth / 2);
	}
	
	private void addLabel(Team team, int middleX) {
		InterfaceLabel teamLabel = new InterfaceLabel(team.getFraction().toString() + " Team", labelFont);
		teamLabel.setHeight(labelHeight);
		
		int x = middleX - (labelFont.getWidth(teamLabel.getText()) / 2);
		teamLabel.setXY(x, content.getHeight() - labelHeight);
		GUISource.setTextColor(teamLabel, Color.WHITE);
		addWidget(teamLabel);
	}
}